<template>
  <!--  缴费项目 -->
  <div class="swipe_details">

    <div class="fanshionable_title">
      <span>缴费项目</span>
    </div>
    <el-form :inline="true" :model="elform" class="demo-form-inline1">
      <el-form-item>
        <el-button type="primary" @click="showPass(true)" size="mini" icon="el-icon-plus">添加</el-button>
      </el-form-item>
      <!-- <el-form-item>
        <el-button
          type="primary"
          @click="onSubmit"
          size="mini"
          >查询</el-button
        >
      </el-form-item>-->
    </el-form>
    <div class="tablemain">
        <!-- v-loading="loading" -->
      <el-table
        :data="detailslist"
        ref="table"
        border
        highlight-current-row
        style="width: 100%"
        :header-cell-style="headFirst"
        :cell-style="(res) => headFirst_col(res, [])"
      >
        <el-table-column prop="paymentName" label="项目名称"></el-table-column>
        <el-table-column prop="amount" label="缴费金额(元)">
          <template slot-scope="scope">{{ scope.row.amount | amountyuan }}</template>
        </el-table-column>
        <el-table-column prop="endTime" label="截止日期" min-width="100px"></el-table-column>
        <el-table-column prop="remark" label="备注"></el-table-column>
        <el-table-column label="操作" width="280px">
          <template slot-scope="scope">
            <el-button @click="roleEdit(scope.row)" type="text" size="small">缴费人员管理</el-button>
            <el-button @click="paymentNotice(scope.row)" type="text" size="small">通知项目人员缴费</el-button>
            <el-button @click="showPass(false, scope.row)" type="text" size="small">修改</el-button>
            <el-button @click="handleEle(scope.row)" type="text" size="small" style="color: red">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChangepage"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="recordCount"
        ></el-pagination>
      </div>
    </div>
    <!-- 编辑缴费项目 -->
    <el-dialog
      :title="operation ? '添加缴费项目' : '修改缴费项目'"
      :visible.sync="subscribe"
      width="500px"
      center
      :close-on-click-modal="false"
      :before-close="closeDialog"
      append-to-body
    >
      <el-form :model="addFrom" :rules="rules" ref="addFrom" label-width="100px" v-if="subscribe">
        <el-form-item label="项目名称:" prop="paymentName">
          <el-col :span="20">
            <el-input size="mini" v-model="addFrom.paymentName" style="width: 180px"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="缴费金额:" prop="amount">
          <el-col :span="24">
            <!-- <el-input
              size="mini"
              v-model.number="addFrom.amount"
              style="width: 180px"
              oninput="value=value.replace(/[^\d]/g,'')"
            ></el-input>-->
            <el-input-number
              v-model="addFrom.amount"
              :min="0.01"
              label="缴费金额"
              size="mini"
              :precision="2"
            ></el-input-number>
            <span>元</span>
          </el-col>
        </el-form-item>
        <el-form-item label="截止日期:" prop="endTime">
          <el-date-picker
            style="width: 130px"
            v-model="addFrom.endTime"
            value-format="yyyy-MM-dd"
            type="date"
            size="mini"
            :clearable="false"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="备注:" prop="remark">
          <el-col :span="20">
            <el-input type="textarea" size="mini" v-model="addFrom.remark" style="width: 180px"></el-input>
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="subscribeAdd()" v-if="operation">确 定</el-button>
        <el-button type="primary" @click="subscribeEdit()" v-if="!operation">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 房间人员加入弹窗 -->
    <add-room-jon v-if="addroomjonVisible" ref="addroomjoin" @refreshDataList="refreshDataList"></add-room-jon>
    <add-dep-jon v-if="adddepjonVisible" ref="adddepjoin" @refreshDataList="addDepartment"></add-dep-jon>
    <add-group-jon v-if="addgroupjonVisible" ref="addgroupjoin" @refreshDataList="addGroupartment"></add-group-jon>
    <div class="roleDialog">
      <!-- 缴费项目人员管理 -->
      <el-dialog
        :before-close="eidtcloseDialog"
        width="900px"
        center
        :title="'缴费项目人员管理'+'（'+editForm.paymentName+ '）'"
        :close-on-click-modal="false"
        :visible.sync="eidtDialogFormVisible"
      >
        <div>
          <el-form :model="editForm" ref="editForm" class="demo-ruleForm" :inline="true">
            <!-- <el-form-item label="缴费项目名称：" prop="paymentName">
              <el-col :span="24">
                {{ editForm.paymentName }}
              </el-col>
            </el-form-item>-->
            <el-form-item label-width="140px">
              <el-button
                type="primary"
                @click="joinpeople"
                size="mini"
                style="margin-bottom: 10px"
                icon="el-icon-plus"
              >添加人员</el-button>
              <!-- <el-button
                type="primary"
                @click="addDepartmentPersonnel"
                size="mini"
                style="margin-bottom: 10px"
              >填加整个部门人员</el-button>
              <el-button
                type="primary"
                @click="addGroupartmentPersonnel"
                size="mini"
                style="margin-bottom: 10px"
              >填加整个分组人员</el-button>
              <el-button
                type="primary"
                @click="addDepartment([])"
                size="mini"
                style="margin-bottom: 10px"
              >填加整个学校人员</el-button> -->
            </el-form-item>
          </el-form>
          <!-- 缴费过滤条件 -->
          <el-form :model="payForm" ref="payForm" class="demo-ruleForm" :inline="true">
            <el-form-item label="姓名：" prop="userName">
              <el-input v-model="payForm.userName" placeholder="请输入姓名" size="mini"></el-input>
            </el-form-item>
            <el-form-item prop="paymentState">
              <el-checkbox v-model="payForm.paymentState">是否已缴费</el-checkbox>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" size="mini" @click="queryPayPeople">查询</el-button>
            </el-form-item>
          </el-form>
          <el-table
            ref="multipleTable"
            :data="tableData"
            tooltip-effect="dark"
            style="width: 100%"
            :stripe="true"
            border
            :header-cell-style="headFirst"
            :cell-style="(res) => headFirst_col(res, [])"
          >
            <!-- <el-table-column type="selection" width="45" align="center"></el-table-column> -->
            <el-table-column prop="departmentName" label="部门" align="center"></el-table-column>
            <el-table-column prop="userNumber" label="编号" align="center"></el-table-column>
            <el-table-column prop="userName" label="姓名" align="center"></el-table-column>
            <el-table-column prop="paymentAmount" label="需缴费金额(元)">
              <template slot-scope="scope">{{ scope.row.paymentAmount | amountyuan }}</template>
            </el-table-column>
            <!-- <el-table-column prop="cardNumber" label="卡号" align="center"></el-table-column> -->
            <!-- <el-table-column prop="userNumber" label="编号" align="center"></el-table-column> -->

            <!-- <el-table-column prop="userSex" label="性别" align="center">
              <template slot-scope="scope">
                {{
                scope.row.userSex | userSexType
                }}
              </template>
            </el-table-column>-->
            <el-table-column prop="paymentState" label="缴费状态" align="center">
              <template slot-scope="scope">
                {{
                scope.row.paymentState == 1?'已缴费':'未交费'
                }}
              </template>
            </el-table-column>

            <el-table-column label="操作" width="120" align="center">
              <template slot-scope="scope">
                <el-button type="text" @click.stop="userNotice(scope.row)" size="small">通知缴费</el-button>
                <el-button
                  type="text"
                  @click.stop="deletePay(scope.row.userGuid)"
                  size="small"
                  style="color: red"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <div class="page">
            <el-pagination
              @size-change="PayhandleSizeChange"
              @current-change="PayhandleCurrentChangepage"
              :current-page.sync="PaycurrentPage"
              :page-sizes="[5, 10, 15]"
              :page-size="PaypageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="PayrecordCount"
            ></el-pagination>
          </div>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import FileSaver from 'file-saver'
import XLSX from 'xlsx'
import bus from '@/bus.js'
import AddRoomJon from '@/components/room-personnel-add-join'
import AddDepJon from '@/components/department-personnel-add-join'
import AddGroupJon from '@/components/group-personnel-add-join'

import { Loading } from 'element-ui'
export default {
  data() {
    return {
      // 缴费项目添加弹窗
      addDialogFormVisible: false,
      // 缴费项目修改
      editDialogFormVisible: false,
      /**加入整个部门的人 */
      adddepjonVisible: false,
      /**加入整个分组的人 */
      addgroupjonVisible: false,
      // 加入人员弹窗
      addroomjonVisible: false,
      // 当前需要修改缴费项目的信息
      editForm: {},
      // 缴费项目中的人员信息
      tableData: [],
      // 缴费项目人员管理弹框
      eidtDialogFormVisible: false,
      // 缴费项目里面的人员分页
      PaypageNum: 1,
      PaypageSize: 10,
      PaycurrentPage: 1,
      PayrecordCount: 0,
      // 缴费查询条件
      payForm: {
        userName: '',
        paymentState: 0
      },
      elform: {},
      //  缴费项目数据
      detailslist: [],
      loading: false,
      pageNum: 1,
      pageSize: 20,
      currentPage: 1,
      recordCount: 0,
      searchInfo: {},

      //导出数据
      detailslistExportlist: [],
      /* 弹框显示*/
      // true为添加，false为修改
      operation: true,
      subscribe: false,
      addFrom: {
        /**
         * 缴费金额
         */
        amount: 100,
        /**
         * 项目名称
         */
        paymentName: '',
        /**
         * 备注
         */
        remark: ''
      },
      rules: {
        amount: [
          { required: true, message: '请输入缴费金额', trigger: 'blur' }
        ],
        paymentName: [
          { required: true, message: '请输入项目名称', trigger: 'blur' }
        ],
        endTime:[
          { required: true, message: '请选择截止时间', trigger: 'blur' }
        ],
      }
    }
  },
  components: {
    AddRoomJon,
    AddDepJon,
    AddGroupJon
  },
  created() {
    this.userGuid = sessionStorage.getItem('administrator')
    this.onSubmit()
  },
  mounted() {},
  computed: {
    changeschoolid: {
      get() {
        return this.$store.state.user.changeschoolid
      }
    }
  },
  methods: {
    // 取消添加弹窗
    closeDialog() {
      this.$refs['addFrom'].resetFields()
      this.subscribe = false
    },
    // 点击添加后弹出的窗口
    showPass(type, row) {
      this.subscribe = true
      if (type) {
        console.log(1)
        this.operation = true
        this.addFrom = {
          amount: 100,
          paymentName: '',
          remark: '',
          endTime:this.formatDate(new Date(), 'yyyy-MM-dd')
        }
      } else {
        this.operation = false
        // this.$set(this.addFrom, "amount", row.amount);
        // this.$set(this.addFrom, "paymentName", row.paymentName);
        // this.$set(this.addFrom, "remark", row.remark);
        console.log(row)
        this.addFrom = {
          ...row,
          amount: row.amount / 100,
          endTime: this.formatDate(new Date(row.endTime), 'yyyy-MM-dd')
        }
      }
    },
    // 确定添加
    subscribeAdd() {
      this.$http({
        url: this.$http.adornUrl('/payment/payment-type-add'),
        method: 'post',
        data: this.$http.adornData({
          messageContent: {
            remark: this.addFrom.remark,
            amount: this.addFrom.amount * 100,
            paymentName: this.addFrom.paymentName,
            endTime: this.addFrom.endTime + ' 23:59:59',
            unitGuid: this.changeschoolid
          },
          messageType: 0
        })
      }).then(({ data }) => {
        if (data.messageCode == 200) {
          this.$message({
            message: '添加成功',
            type: 'success'
          })
          this.getList()
          this.subscribe = false
        } else {
          this.$message.error(data.errorMessage)
        }
      })
    },
    // 确定修改
    subscribeEdit() {
      this.$http({
        url: this.$http.adornUrl('/payment/payment-type-update'),
        method: 'post',
        data: this.$http.adornData({
          messageContent: {
            paymentGuid: this.addFrom.paymentGuid,
            amount: this.addFrom.amount * 100,
            paymentName: this.addFrom.paymentName,
            endTime: this.addFrom.endTime + ' 23:59:59',
            remark: this.addFrom.remark,
            unitGuid: this.changeschoolid
          },
          messageType: 0
        })
      }).then(({ data }) => {
        if (data.messageCode == 200) {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          this.getList()
          this.subscribe = false
        } else {
          this.$message.error(data.errorMessage)
        }
      })
    },
    // 删除
    handleEle(row) {
      this.$confirm('此操作将永久删除该缴费项目, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          console.log(row.paymentGuid)
          this.$http({
            url: this.$http.adornUrl('/payment/payment-type-delete'),
            method: 'post',
            data: this.$http.adornData({
              messageContent: {
                paymentGuid: row.paymentGuid,
                unitGuid: this.changeschoolid
              },
              messageType: 0
            })
          }).then(({ data }) => {
            if (data.messageCode == 200) {
              this.onSubmit()
              this.$message({
                message: '删除成功',
                type: 'success'
              })
            } else {
              this.$message.error(data.errorMessage)
            }
          })
        })
        .catch(() => {})
    },
    // 查询
    onSubmit() {
      let searchInfo = {}
      searchInfo.unitGuid = this.changeschoolid
      this.searchInfo = searchInfo
      this.pageNum = 1
      this.getList()
    },

    // 获取数据
    getList() {
      this.$http({
        url: this.$http.adornUrl('/payment/payment-type-page'),
        method: 'post',
        data: this.$http.adornData({
          messageContent: {
            asc: true,
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            searchInfo: this.searchInfo
          },
          messageType: 0
        })
      }).then(({ data }) => {
        if (data.messageCode == 200) {
          //   this.$message({
          //     message: "查询成功",
          //     type: "success",
          //   });

          this.recordCount = data.messageContent.recordCount
          this.currentPage = data.messageContent.pageNum
          this.detailslist = data.messageContent.data
          console.log(data.messageContent.data)
        } else {
          this.$message.error(data.errorMessage)
        }
      })
    },

    handleSizeChange(val) {
      this.pageSize = val
      this.getList()
    },
    handleCurrentChangepage(val) {
      this.pageNum = val
      this.getList()
    },
    /**
     * 缴费通知
     */
    // 项目通知
    paymentNotice(row) {
      this.$http({
        url: this.$http.adornUrl('/payment/notice-all-payment'),
        method: 'post',
        data: this.$http.adornData({
          messageContent: {
            paymentGuid: row.paymentGuid,
            unitGuid: this.changeschoolid
          },
          messageType: 0
        })
      }).then(({ data }) => {
        if (data.messageCode == 200) {
          this.onSubmit()
          this.$message({
            message: '已发生消息至项目中的人员',
            type: 'success'
          })
        } else {
          this.$message.error(data.errorMessage)
        }
      })
    },
    // 个人通知
    userNotice(row) {
      this.$http({
        url: this.$http.adornUrl('/payment/notice-user-payment'),
        method: 'post',
        data: this.$http.adornData({
          messageContent: {
            paymentGuid: this.editPayInfo.paymentGuid,
            userGuid: row.userGuid,
            unitGuid: this.changeschoolid
          },
          messageType: 0
        })
      }).then(({ data }) => {
        if (data.messageCode == 200) {
          this.onSubmit()
          this.$message({
            message: '已发生消息至项目中的人员',
            type: 'success'
          })
        } else {
          this.$message.error(data.errorMessage)
        }
      })
    },
    /**
     * 人员管理
     */
    roleEdit(row) {
      // 当前修改的缴费项目信息
      this.editPayInfo = row
      this.$set(this.editForm, 'paymentName', row.paymentName)
      this.payForm = {
        userName: '',
        paymentState: 0
      }
      this.PaypageNum = 1
      this.getPayPeople()
      this.eidtDialogFormVisible = true
    },
    eidtcloseDialog(done) {
      this.eidtDialogFormVisible = false
      done()
    },
    queryPayPeople() {
      this.PaypageNum = 1
      this.getPayPeople()
    },
    // 缴费项目绑定人员查询
    getPayPeople() {
      this.$http({
        url: this.$http.adornUrl('/payment/payment-user-page'),
        method: 'post',
        data: this.$http.adornData({
          messageContent: {
            asc: true,
            pageNum: this.PaypageNum,
            pageSize: this.PaypageSize,
            searchInfo: {
              paymentGuid: this.editPayInfo.paymentGuid,
              unitGuid: this.changeschoolid,
              userName: this.payForm.userName,
              paymentState: this.payForm.paymentState ? 1 : 0
            }
          },
          messageType: 0
        })
      }).then(({ data }) => {
        if (data.messageCode == 200) {
          this.tableData = data.messageContent.data || []
          this.PayrecordCount = data.messageContent.recordCount
          this.PaycurrentPage = data.messageContent.pageNum
          if (this.tableData.length == 0) {
            this.$message({
              message: '查询成功该缴费项目当前暂无绑定人员',
              type: 'success'
            })
          }
        } else {
          this.$message.error(data.errorMessage)
        }
      })
    },
    // 缴费项目里面的人员分页
    PayhandleSizeChange(val) {
      this.PaypageSize = val
      this.getPayPeople()
    },
    PayhandleCurrentChangepage(val) {
      this.PaypageNum = val
      this.getPayPeople()
    },
    /**绑定人员 */
    // 加入人员弹窗
    joinpeople() {
      this.addroomjonVisible = true
      this.$nextTick(() => {
        this.$refs.addroomjoin.addinit('/payment/payment-user-by-list', {
          paymentGuid: this.editPayInfo.paymentGuid
        })
      })
    },
    // 加入人员点击确定后触发
    refreshDataList(val) {
      this.addroomjonVisible = false
      console.log(this.editPayInfo)
      if (val.length > 0) {
        this.setPayPeople(val)
      }
    },
    // 缴费项目手下的人员添加
    setPayPeople(val) {
      this.$http({
        url: this.$http.adornUrl('/payment/payment-user-assign-user'),
        method: 'post',
        data: this.$http.adornData({
          messageContent: {
            paymentGuid: this.editPayInfo.paymentGuid,
            unitGuid: this.changeschoolid,
            userGuidList: val
          },
          messageType: 0
        })
      }).then(({ data }) => {
        if (data.messageCode == 200) {
          this.$message({
            message: '添加成功',
            type: 'success'
          })
          this.queryPayPeople()
        } else {
          this.$message.error(data.errorMessage)
        }
      })
    },
    /** */
    // 将整部部门的人员加入该缴费项目
    addDepartmentPersonnel() {
      this.adddepjonVisible = true
      this.$nextTick(() => {
        this.$refs.adddepjoin.addinit('')
      })
    },
    // 确定后触发
    addDepartment(val) {
      // 批量加载条
      this.loading = Loading.service({
        lock: true,
        text: '正在添加人员中，请稍等...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      console.log(val)
      // 缴费项目手下的人员添加
      this.$http({
        url: this.$http.adornUrl('/payment/payment-user-assign-department'),
        method: 'post',
        data: this.$http.adornData({
          messageContent: {
            paymentGuid: this.editPayInfo.paymentGuid,
            unitGuid: this.changeschoolid,
            departmentGuidList: val
          },
          messageType: 0
        })
      }).then(({ data }) => {
        // 关闭加载条
        this.loading.close()
        if (data.messageCode == 200) {
          this.$message({
            message:
              val.length != 0
                ? '已将选中部门中人员全部添加缴费项目中'
                : '已将该学校中所有人员全部添加缴费项目中',
            type: 'success'
          })
          this.queryPayPeople()
        } else {
          // 关闭加载条
          this.loading.close()
          this.$message.error(data.errorMessage)
        }
      })
    },
    // 移除项目中的人员
    deletePay(userGuid) {
      this.$confirm('此操作将此人员移除该项目, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl('/payment/payment-user-relieve'),
            method: 'post',
            data: this.$http.adornData({
              messageContent: {
                paymentGuid: this.editPayInfo.paymentGuid,
                unitGuid: this.changeschoolid,
                userGuid: userGuid
              },
              messageType: 0
            })
          }).then(({ data }) => {
            console.log(data)
            if (data.messageCode == 200) {
              this.$message({
                message: '删除成功',
                type: 'success'
              })
              this.queryPayPeople()
            } else {
              this.$message.error(data.errorMessage)
            }
          })
        })
        .catch(() => {})
    },

    /** */
    // 将整部分组的人员加入该缴费项目
    addGroupartmentPersonnel() {
      this.addgroupjonVisible = true
      this.$nextTick(() => {
        this.$refs.addgroupjoin.addinit()
      })
    },
    // 确定后触发
    addGroupartment(val) {
      // 批量加载条
      this.loading = Loading.service({
        lock: true,
        text: '正在添加人员中，请稍等...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      console.log(val)
      // 缴费项目手下的人员添加
      this.$http({
        url: this.$http.adornUrl('/payment/payment-user-assign-department'),
        method: 'post',
        data: this.$http.adornData({
          messageContent: {
            paymentGuid: this.editPayInfo.paymentGuid,
            unitGuid: this.changeschoolid,
            groupGuidList: val
          },
          messageType: 0
        })
      }).then(({ data }) => {
        // 关闭加载条
        this.loading.close()
        if (data.messageCode == 200) {
          this.$message({
            message: '已将选中分组中人员全部添加缴费项目中',
            type: 'success'
          })
          this.queryPayPeople()
        } else {
          // 关闭加载条
          this.loading.close()
          this.$message.error(data.errorMessage)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.swipe_details {
  .el-dialog__body .el-form.el-form--inline {
    width: 100%;
  }
  padding: 10px;
  overflow: hidden;
  .fanshionable_title {
    margin-top: 20px;
    height: 30px;
    color: #909399;
    border-bottom: 1px solid #e5e9f2;
  }
  //滚动条的宽度
  ::-webkit-scrollbar {
    width: 10px;
    height: 10px;
  }

  //滚动条的滑块
  ::-webkit-scrollbar-thumb {
    background-color: #a1a3a9;
    border-radius: 3px;
  }
  .el-table__row .el-table__row--level-1 {
    background-color: #e5e9f2;
  }
  /deep/.el-dialog--center .el-dialog__body {
    padding-left: 70px;
  }
  .demo-form-inline,
  .demo-form-inline1 {
    height: 40px;
    display: flex;
    /deep/.el-form-item__label {
      padding-right: 5px;
    }
    .el-form-item {
      margin-bottom: 0px;
    }
    .el-date-editor.el-input,
    .el-date-editor.el-input__inner {
      width: 340px;
    }
  }
  .demo-form-inline1 {
    border-bottom: 1px solid #e5e9f2;
    .searchmarigin {
      margin-left: 20px;
    }
  }
  // .demo-ruleForm {
  //   margin-left: -70px;
  // }
  .roleDialog {
    .el-form-item {
      margin-bottom: 0;
    }
    /deep/.el-dialog--center .el-dialog__body {
      padding: 20px;
      padding-top: 0;
    }
  }
  .page {
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>